import datetime
import json
import os

filename = "./log/assistant_qa.log"

with open(filename, "r") as f:
    lines = f.readlines()

# 写出到excel
from openpyxl import Workbook
workbook = Workbook()
ass_sheet = workbook.create_sheet(title="assistant_qa")
all_sheet =workbook.create_sheet(title="all_qa")

ass_sheet["A1"] = "评价"
ass_sheet["B1"] = "反馈意见"
ass_sheet["C1"] = "反馈时间"
ass_sheet["D1"] = "问题"
ass_sheet["E1"] = "回答"

all_sheet["A1"] = "问题"
all_sheet["B1"] = "答案"

for i, line in enumerate(lines):
    i += 2
    line = json.loads(line)
    ass_sheet[f"A{i}"] = line["rating"]
    ass_sheet[f"B{i}"] = line["describe"]
    ass_sheet[f"C{i}"] = datetime.datetime.fromtimestamp(line["timestamp"] / 1000)
    ass_sheet[f"D{i}"] = line["query"]
    ass_sheet[f"E{i}"] = line["answer"]


filename = "./log/all_qa.log"

with open(filename, "r") as f:
    lines = f.readlines()

qa = {}
import ast
for line in lines:
    line = ast.literal_eval(line.strip())
    if "query" in line:
        qa[line["id"]] = {"query": line["query"], "answer": None}
    if "answer" in line:
        qa[line["id"]]["answer"] = line["answer"]

for i, value in enumerate(qa.values()):
    i += 2
    all_sheet[f"A{i}"] = value["query"]
    all_sheet[f"B{i}"] = value["answer"]


workbook.save("assistant_qa.xlsx")

workbook.close()